/**
 * 
 */
package com.wegax.finprod.accountcard;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField;
import com.wegax.finprod.message.MessageCodes;
import com.wegax.finprod.message.Messages;
import com.wegax.finprod.wizardform.WizardForm;
import com.wegax.finprod.wizardform.WizardFormStep;

/**
 * @author wegax
 * 
 */
class ProductInformationWizardStep extends FormLayout implements WizardFormStep<PersonalAccountDebitCardMapper> {

	private static final Logger log = LoggerFactory.getLogger(ProductInformationWizardStep.class);

	private static final String ACCOUNTTYPE_FIELD = "accountType";

	private static final String ACCOUNTCURRENCY_FIELD = "accountCurrency";

	private static final String PAYCARDAVAILABILITY_FIELD = "paycardAvailability";

	private static final String PAYCARDKIND_FIELD = "paycardKind";

	private static final String EMBOSS_FIELD = "emboss";

	private WizardForm<PersonalAccountDebitCardMapper> wizardForm;

	private ComboBox accountTypeField;

	private ComboBox accountCurrencyField;

	private OptionGroup paycardAvailabilityField;

	private ComboBox paycardKindField;

	private TextField embossField;

	private Form form;

	public ProductInformationWizardStep() {
		initComponents();
	}

	private void initComponents() {
		form = new Form();
		addComponent(form);

		accountTypeField = new ComboBox(Messages.getString(MessageCodes.ACCOUNT_TYPE_FIELD_CAPTION));
		accountTypeField.setRequired(true);
		accountTypeField.setRequiredError(Messages.getString(MessageCodes.ACCOUNT_TYPE_FIELD_REQUIRED_MESSAGE));
		for (AccountType accType : AccountType.values()) {
			Item accTypeItem = accountTypeField.addItem(accType);
			accountTypeField.setItemCaption(accType, Messages.getString(MessageCodes.ACCOUNT_TYPE_PREFIX + accType));
		}
		form.addField(ACCOUNTTYPE_FIELD, accountTypeField);

		accountCurrencyField = new ComboBox(Messages.getString(MessageCodes.ACCOUNT_CURRENCY_FIELD_CAPTION));
		accountCurrencyField.setRequired(true);
		accountCurrencyField.setRequiredError(Messages.getString(MessageCodes.ACCOUNT_CURRENCY_FIELD_REQUIRED_MESSAGE));
		for (CurrencyType currencyType : CurrencyType.values()) {
			Item currencyTypeItem = accountCurrencyField.addItem(currencyType);
			accountCurrencyField.setItemCaption(currencyType,
					Messages.getString(MessageCodes.ACCOUNT_CURRENCY_PREFIX + currencyType));
		}
		form.addField(ACCOUNTCURRENCY_FIELD, accountCurrencyField);

		paycardAvailabilityField = new OptionGroup("", Arrays.asList(Messages
				.getString(MessageCodes.PAYCARD_AVAILABILITY)));
		paycardAvailabilityField.setMultiSelect(true);
		paycardAvailabilityField.setImmediate(true);
		paycardAvailabilityField.addListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				Set<?> values = (Set<?>) event.getProperty().getValue();
				if (!values.isEmpty()) {
					processActivationOfPaycardFields(true);
				} else {
					processActivationOfPaycardFields(false);
				}
			}
		});
		form.addField(PAYCARDAVAILABILITY_FIELD, paycardAvailabilityField);

		paycardKindField = new ComboBox(Messages.getString(MessageCodes.PAYCARD_KIND_FIELD_CAPTION));
		paycardKindField.setRequiredError(Messages.getString(MessageCodes.PAYCARD_KIND_FIELD_REQUIRED_MESSAGE));
		for (PaycardType paycardType : PaycardType.values()) {
			Item currencyTypeItem = paycardKindField.addItem(paycardType);
			paycardKindField.setItemCaption(paycardType,
					Messages.getString(MessageCodes.PAYCARD_KIND_PREFIX + paycardType));
		}
		form.addField(PAYCARDKIND_FIELD, paycardKindField);

		embossField = new TextField(Messages.getString(MessageCodes.EMBOSS_FIELD_CAPTION));
		embossField.addValidator(new StringLengthValidator(Messages
				.getString(MessageCodes.EMBOSS_FIELD_LENGTH_VALIDATION_MESSAGE), 0, 60, true));
		form.addField(EMBOSS_FIELD, embossField);

		processActivationOfPaycardFields(false);
	}

	private void processActivationOfPaycardFields(boolean active) {
		paycardKindField.setVisible(active);
		paycardKindField.setRequired(active);
		embossField.setVisible(active);

		if (!active) {
			paycardKindField.setValue(null);
			embossField.setValue("");
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getCaption()
	 */
	@Override
	public String getCaption() {
		return Messages.getString(MessageCodes.PRODUCT_INFORMATION_STEP_CAPTION);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getContent()
	 */
	@Override
	public Component getContent() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onAdvance()
	 */
	@Override
	public boolean onAdvance() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onBack()
	 */
	@Override
	public boolean onBack() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#getForm()
	 */
	@Override
	public Form getForm() {
		return form;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#map(java.lang.Object)
	 */
	@Override
	public void map(PersonalAccountDebitCardMapper bean) {
		bean.setAccountType((AccountType) accountTypeField.getValue());
		bean.setCurrencyType((CurrencyType) accountCurrencyField.getValue());
		if (!((Collection<?>) paycardAvailabilityField.getValue()).isEmpty()) {
			bean.setPaycardKindAvailability(true);
			bean.setPaycardType((PaycardType) paycardKindField.getValue());
			bean.setEmboss((String) embossField.getValue());
		} else {
			bean.setPaycardKindAvailability(false);
			bean.setPaycardType(null);
			bean.setEmboss(null);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wegax.finprod.wizardform.WizardFormStep#initFormStep(com.wegax.finprod.wizardform.WizardForm)
	 */
	@Override
	public void initFormStep(WizardForm<PersonalAccountDebitCardMapper> wizardForm) {
		this.wizardForm = wizardForm;
	}

	/* (non-Javadoc)
	 * @see com.wegax.finprod.wizardform.WizardFormStep#onShow()
	 */
	@Override
	public void onShow() {
		
	}
}
